<?php

class Db_Tables2_BMailList
{
	function __construct() {
		$this->dealers_db = Zend_Registry::get("dealers_db");
	}
	
	function SendServiceNotification($contract_id)
	{
		$result = FALSE;
		
		if($contract_id)
		{
			$select = $this->dealers_db->select();
			$select->from("b_contract as t", array("sales_emp_id", "client_id", "contract_sid", "client_name", "sales_emp_name", "tips"));
			$select->joinLeft("b_client as c", "c.client_id=t.client_id", array("is_dealer"));
			$select->where("t.contract_id = ?", $contract_id);
			$contract_row = $this->dealers_db->fetchRow($select);
			
			$mod_users = new Db_Tables2_User();
			$my_leaders = $mod_users->GetMyLeaders($contract_row['sales_emp_id']);
			
			$mod_client = new Db_Tables2_BClient();
			$client_additional_info = $mod_client->FetchClientDetail($contract_row['client_id'], 1);
			
			$select = $this->dealers_db->select();
			$select->from("b_contract_sku", "*");
			$select->where("contract_id = ?", $contract_id);
			$sku_rows = $this->dealers_db->fetchAll($select);
			
			if(!empty($sku_rows))
			{
				$mod_mail_log = new Db_Tables2_BLogsMailSent();
				$mod_sku = new Db_Tables2_BSku();
				
				foreach($sku_rows as $row)
				{
					$mod_params = new Db_Tables2_BDictParams();
					$domain = $mod_params->GetVal("DOMAIN");
					$primary_txt = "<br><a href='".$domain."login/confirm-contract-sku/token/".$row['mail_token']."'>【点击此处确认上线】</a><br>";
					
					$sku_path = $mod_sku->GetSkuPath($row['sku_id']);
					
					//generate txt
					$subjet = "【商务系统】".$contract_row['client_name'].$row['product_name']."上线通知，合同编号：".$contract_row['contract_sid'];
					
					$body = "<br>客户全称：".$contract_row['client_name']."<br>";
					
					if($contract_row['is_dealer'])
					{
						$body .= "经销商ID：".$contract_row['is_dealer']."<br>";
					}
					
					$body .= "品牌：".$client_additional_info['additional_info']['brand_info']['str']."<br>";
					$body .= "厂商：".$client_additional_info['additional_info']['factory_info']['str']."<br>";
					$body .= "渠道：".$client_additional_info['additional_info']['channel_info']['str']."<br>";
					$body .= "产品：".$sku_path['path_name']."<br>";
					$body .= "数量：".$row['quantity']."<br>";
					$body .= "服务期限：".$row['start_date']." 至 ".$row['expired_date']."<br>";
					$body .= "销售顾问：".$contract_row['sales_emp_name']."<br>";
                    $body .= "备注：".$contract_row['tips']."<br>";

                    if(isset($my_leaders[0]))
					{
						$body .= "销售主管：".$my_leaders[0]['emp_name']."<br>";
					}
					
					//if extra platform info
					$select = $this->dealers_db->select();
					$select->from("b_contract_sku_service", "*");
					$select->where("cs_id = ?", $row['cs_id']);
					$service_rows = $this->dealers_db->fetchAll($select);
					
					if(!empty($service_rows))
					{
						$body .= "<br>";
						$body .= "【托管平台】<br>";
						
						foreach($service_rows as $service_row)
						{
							$body .= $service_row['service_name']." ".$service_row['start_date']." 至 ".$service_row['expired_date']."<br>";
						}
					}
                    
                    $mod_params = new Db_Tables2_BDictParams();
                    $sys_id = $mod_params->GetSysId();
					
					//add to log
					$select = $this->dealers_db->select();
					$select->from("dealers_db".$sys_id.".b_mail_list as m", array("emp_id", "is_primary"));
					$select->joinLeft("hxb_db".$sys_id.".auth_emp as e", "e.id=m.emp_id", array("email"));
					$select->where("m.product_id = ?", $row['product_id']);
					$select->where("m.status = ?", 1);
					$mail_rows = $this->dealers_db->fetchAll($select);
					
					if(!empty($mail_rows))
					{
						foreach($mail_rows as $mail_row)
						{
							if($mail_row['email'])
							{	
								$mod_mail_log->mail_address = $mail_row['email'];
								$mod_mail_log->title = $subjet;
								
								if($mail_row['is_primary'])
								{
									$mod_mail_log->contents = $body.$primary_txt;
								}else{
									$mod_mail_log->contents = $body;
								}

								$mod_mail_log->addRecord();
							}
						}
					}
				}
				
				$result = TRUE;
			}
		}
		
		return $result;
	}

    /**
     * 发送邮件通知给销售
     * @param $contract_id
     * @return bool
     */
    public function SendServiceNotificationToSales($contract_id)
    {
        $result = FALSE;

        if($contract_id) {
            $select = $this->dealers_db->select();
            $select->from("b_contract as t", array("sales_emp_id", "client_id", "client_name", "sales_emp_name", "tips", "allow_edit", "status"));
			$select->joinLeft("b_client as c", "c.client_id=t.client_id", array("is_dealer"));
            $select->where("t.contract_id = ?", $contract_id);
            $contract_row = $this->dealers_db->fetchRow($select);

            $mod_users = new Db_Tables2_User();
            $emp_info = $mod_users->GetAuthEmpInfo($contract_row['sales_emp_id']);

            // for test
            //$emp_info["email"] = "zhaoqixiang@xiongmaocar.com";
            $mod_client = new Db_Tables2_BClient();
            $client_additional_info = $mod_client->FetchClientDetail($contract_row['client_id'], 1);

            $select = $this->dealers_db->select();
            $select->from("b_contract_sku", "*");
            $select->where("contract_id = ?", $contract_id);
            $sku_rows = $this->dealers_db->fetchAll($select);

            if (!empty($sku_rows)) {
                $mod_mail_log = new Db_Tables2_BLogsMailSent();
                $mod_sku = new Db_Tables2_BSku();

                foreach ($sku_rows as $row) {
                    $sku_path = $mod_sku->GetSkuPath($row['sku_id']);
                    //generate txt
                    $subjet = "【商务系统】" . $contract_row['client_name'] . $row['product_name'] . "合同状态更新";
                    $body = "<br>客户全称：" . $contract_row['client_name'] . "，合同状态更新，请注意关注合同状态<br>";
					
					if($contract_row['is_dealer'])
					{
						$body .= "经销商ID：".$contract_row['is_dealer']."<br>";
					}
					
                    $body .= "品牌：" . $client_additional_info['additional_info']['brand_info']['str'] . "<br>";
                    $body .= "厂商：" . $client_additional_info['additional_info']['factory_info']['str'] . "<br>";
                    $body .= "渠道：" . $client_additional_info['additional_info']['channel_info']['str'] . "<br>";
                    $body .= "产品：" . $sku_path['path_name'] . "<br>";
                    $body .= "数量：" . $row['quantity'] . "<br>";
                    $body .= "服务期限：" . $row['start_date'] . " 至 " . $row['expired_date'] . "<br>";
                    $body .= "销售顾问：" . $contract_row['sales_emp_name'] . "<br>";
                    $body .= "备注：" . $contract_row['tips'] . "<br>";
                    if($row["product_id"] == 2){
                        $body .= "汇销宝账号：" . $row["hxb_account"] . "<br>";
                    }

                    if($contract_row["allow_edit"]){
                        $body .= "审批状态：驳回修改";
                        $mod_rejection = new Db_Tables2_BContractRejection();
                        $mod_rejection->contract_id = $contract_id;
                        $reject_log = $mod_rejection->GetRecord();
                        $body .= "(" . $reject_log["emp_name"] . "：" . $reject_log["reason"].  ")" . "<br>";

                    } else {
                        $mod_statuses = new Db_Tables2_BDictContractStatus();
                        $statuses = $mod_statuses->GetStatuses();
                        $body .= "审批状态：". $statuses[$contract_row['status']] . "<br>";;
                    }

                    //if extra platform info
                    $select = $this->dealers_db->select();
                    $select->from("b_contract_sku_service", "*");
                    $select->where("cs_id = ?", $row['cs_id']);
                    $service_rows = $this->dealers_db->fetchAll($select);

                    if (!empty($service_rows)) {
                        $body .= "<br>";
                        $body .= "【托管平台】<br>";

                        foreach ($service_rows as $service_row) {
                            $body .= $service_row['service_name'] . " " . $service_row['start_date'] . " 至 " . $service_row['expired_date'] . "<br>";
                        }
                    }


                    if ($emp_info['email']) {
                        $mod_mail_log->mail_address = $emp_info['email'];
                        $mod_mail_log->title = $subjet;
                        $mod_mail_log->contents = $body;
                        $mod_mail_log->addRecord();
                    }
                }

                $result = TRUE;
            }
        }

        return $result;
    }


}